import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SpringLayout;


public class CourierButtonPanel extends JPanel{
	private CourierCanvas canvas;
	
	public CourierButtonPanel(CourierCanvas c) {
		canvas = c;
		
		setMinimumSize(new Dimension(675, 100));
		JPanel buttonRow = new JPanel();
		JButton newPage = new JButton("+ New Page");
		JButton deletePage = new JButton("-- Delete Page");
		JButton backward = new JButton("< Backward");
		JButton forward = new JButton("Forward >");
		JButton undo = new JButton("Undo");
		
		newPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				canvas.newPage();
			}
		});
		deletePage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				canvas.deletePage();
			}
		});
		forward.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				canvas.forward();
			}
		});
		backward.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				canvas.backward();
			}
		});
		
		undo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				canvas.undo();
			}
		});
		
		buttonRow.add(newPage);
		buttonRow.add(deletePage);
		buttonRow.add(backward);
		buttonRow.add(forward);
		buttonRow.add(undo);
		
		JPanel radioButtons = new JPanel();
		JRadioButton freeForm = new JRadioButton("Free-form");
		JRadioButton rectangle = new JRadioButton("Rectangle");
		JRadioButton text = new JRadioButton("Text");
		JRadioButton oval = new JRadioButton("Oval");
		JRadioButton triangle = new JRadioButton("Triangle");
		
		freeForm.setActionCommand("freeform");
		rectangle.setActionCommand("rectangle");
		oval.setActionCommand("oval");
		text.setActionCommand("text");
		triangle.setActionCommand("triangle");
		
		freeForm.addActionListener(canvas);
		rectangle.addActionListener(canvas);
		oval.addActionListener(canvas);
		text.addActionListener(canvas);
		triangle.addActionListener(canvas);
		ButtonGroup objectTypeRadio = new ButtonGroup();
		objectTypeRadio = new ButtonGroup();
		freeForm.setSelected(true);
		
		objectTypeRadio.add(freeForm);
		objectTypeRadio.add(rectangle);
		objectTypeRadio.add(oval);
		objectTypeRadio.add(text);
		objectTypeRadio.add(triangle);
		
		String[] fontSizeExamples = {"8","9","10","11","12","14","16","18","20","24","28","32"};
		JComboBox fontSizeList = new JComboBox(fontSizeExamples);
		fontSizeList.setEditable(true);
		fontSizeList.addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JComboBox cb = (JComboBox) e.getSource();
					    String newSelection = (String) cb.getSelectedItem();
					    canvas.setFontSize(new Integer(newSelection));
					}
		});
			
		fontSizeList.setSelectedIndex(4);
		
		String[] strokeSizeExamples = {"1","2","4","8","16","20","24","36","50","100"};
		JComboBox strokeSizeList = new JComboBox(strokeSizeExamples);
		strokeSizeList.setEditable(true);
		strokeSizeList.addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JComboBox cb = (JComboBox) e.getSource();
					    String newSelection = (String) cb.getSelectedItem();
					    canvas.setStrokeSize(new Integer(newSelection));
					}
		
		});
		
		radioButtons.add(freeForm);
		radioButtons.add(rectangle);
		radioButtons.add(oval);
		radioButtons.add(triangle);
		radioButtons.add(text);
		
		JPanel comboPanel = new JPanel();
		comboPanel.add(new JLabel("Font Size:"));
		comboPanel.add(fontSizeList);
		comboPanel.add(new JLabel("Stroke: "));
		comboPanel.add(strokeSizeList);
		
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		ColorPicker colorPicker = new ColorPicker(canvas);
		buttonPanel.add(buttonRow, BorderLayout.NORTH);
		buttonPanel.add(radioButtons, BorderLayout.CENTER);
		JPanel optionsPanel = new JPanel();
		optionsPanel.setLayout(new BorderLayout());
		optionsPanel.add(buttonPanel, BorderLayout.NORTH);
		optionsPanel.add(comboPanel, BorderLayout.CENTER);
		
		SpringLayout layout = new SpringLayout();
		this.setLayout(layout);
		int pixelDistance = 90;
		layout.putConstraint(SpringLayout.WEST, this, 0, SpringLayout.WEST, colorPicker);
		layout.putConstraint(SpringLayout.EAST, colorPicker, pixelDistance, SpringLayout.WEST, optionsPanel);
		layout.putConstraint(SpringLayout.EAST, this, 0, SpringLayout.EAST, optionsPanel);
		layout.putConstraint(SpringLayout.NORTH, this, 10, SpringLayout.NORTH, colorPicker);
		layout.putConstraint(SpringLayout.NORTH, this, 10, SpringLayout.NORTH, optionsPanel);
		layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, colorPicker);
		layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, optionsPanel);
		this.add(colorPicker);
		this.add(optionsPanel);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		        RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = new Font("Serif", Font.PLAIN, 12);
		g2.setFont(font);
		g2.setColor(Color.black);
		g2.drawString("Page: "+canvas.getPageIndex(), 20, 150);
	}
}
